//
//  CustomSecondCollectionViewCell.m
//  AmountBody
//
//  Created by smm on 2018/1/3.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "CustomSecondCollectionViewCell.h"
//static const CGFloat itemWidth = 60;
//static const CGFloat itemHeight = 60+40;

@implementation CustomSecondCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
    }
    return self;
}
-(void)initSubviews
{
    _headImage = [[UIImageView alloc]init];
    [self addSubview:_headImage];
    _nameLabel = [[UILabel alloc]init];
    [self addSubview:_nameLabel];
    _chooseImage = [[UIImageView alloc]init];
    [self addSubview:_chooseImage];
    
}
-(void)setCellWithModel:( CustomSecondModel*)model andType:(CellType)cellType
{
    _model = model;
    _cellType = cellType;
    _headImage.hidden = YES;
    _chooseImage.hidden = YES;
    _text.hidden = YES;
    _valueLabel.hidden = YES;
    switch (cellType) {
        case CellTypeForNomal:
            {
                [self setNomalCellWithModel:model];
            }
            break;
        case CellTypeForName:
        {
            [self setNameCellWithModel:model];
        }
            break;
        case CellTypeForLabel:
        {
            [self setLabelCellWithModel:model];
        }
            break;
        case CellTypeForText:
        {
            [self setTextCellWithModel:model];
        }
            break;
        case CellTypeForEmbroid:
        {
            [self setEmbroidCellWithModel:model];
        }
            break;
        default:
            break;
    }
    
}

-(void)setCellChoose:(BOOL)cellChoose
{
    switch (_cellType) {
        case CellTypeForName:
        {
            if (cellChoose) {
                _chooseImage.hidden = NO;
                [_nameLabel setBianKuangAndColor:[UIColor clearColor]];
            }else{
                _chooseImage.hidden = YES;
                [_nameLabel setBianKuangAndColor:RGBColorAlpha(205, 205, 205, 1)];
            }
        }
            break;
         case CellTypeForNomal:
        {
            if (cellChoose) {
                _chooseImage.hidden = NO;

            }else{
                _chooseImage.hidden = YES;
            }
        }
            break;
        case CellTypeForText:
        {
            if (cellChoose) {
                _chooseImage.hidden = NO;
                _headImage.userInteractionEnabled = YES;
                
            }else{
                _chooseImage.hidden = YES;
                _headImage.userInteractionEnabled = NO;
            }
        }
            break;
        case CellTypeForEmbroid:
        {
            if (cellChoose) {
                _headImage.image = [UIImage imageNamed:@"cixiu_xuan"];
            }else{
                _headImage.image = [UIImage imageNamed:@"cixiu_weixuan"];
            }
            NSString * str = [NSString stringWithFormat:@"%@",_model.valueWords];
            NSString * str2 = [NSString stringWithFormat:@"\n%@-%@",_model.valueFont,_model.valueColor];
            NSString * needStr = [NSString stringWithFormat:@"%@%@",str,str2];
            NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:needStr];
            
            // 设置字体和设置字体的范围
            [attrStr addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:11.0f]
                            range:NSMakeRange(0, needStr.length)];
            // 添加文字颜色
            [attrStr addAttribute:NSForegroundColorAttributeName
                            value:QianZi_color
                            range:NSMakeRange(0, needStr.length)];
            // 添加文字颜色
            [attrStr addAttribute:NSForegroundColorAttributeName
                            value:ShenZi_Color
                            range:NSMakeRange(0, str.length)];
            // 设置字体和设置字体的范围
            [attrStr addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:13.0f]
                            range:NSMakeRange(0, str.length)];
            if ([GlobalHandleValue(_model.valueWords) isEqualToString:@""]|| GlobalHandleValue(_model.valueWords) == nil) {
                attrStr = [[NSMutableAttributedString alloc] initWithString:@""];
            }
            _valueLabel.attributedText = attrStr;
        }
        default:
            break;
    }
}
-(void)setTextCellWithModel:(CustomSecondModel*)model
{
    CGFloat jianGe = 7;
    [_text removeFromSuperview];
    _headImage.hidden = NO;
    _headImage.frame = CGRectMake(jianGe, jianGe, _item_Width-jianGe*2, _item_Width-jianGe*2);
    [_headImage sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""]];
    _headImage.userInteractionEnabled = NO;
    _text = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, _item_Width-jianGe*2, _item_Width-jianGe*2)];
    _text.text = model.value;
    _text.delegate = self;
    _text.textColor = ZhongZi_Color;
    [_text setBianKuangAndColor:ShenLine_color];
    [_text setYuanJiaoForRadius:5.0 andMasks:NO];
    _text.font = More_Little_Font;
    _text.placeholder = @"请输入内容";
    [_headImage addSubview:_text];
    
    _nameLabel.text = model.name;
    _nameLabel.textColor = QianZi_color;
    _nameLabel.font = More_Little_Font;
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.numberOfLines = 2;
    [_nameLabel setBianKuangAndColor:[UIColor clearColor]];
    _nameLabel.frame = CGRectMake(0,_headImage.frame.origin.y+_headImage.frame.size.height+5, _item_Width, 35);
    if (model.isChoose) {
        _chooseImage.hidden = NO;
    }else{
        _chooseImage.hidden = YES;
    }
    _chooseImage.image = [UIImage imageNamed:@"colorXuan"];
    _chooseImage.frame = CGRectMake(jianGe*2+2, 0, _item_Width-jianGe*2-2, _item_Width-jianGe*2);
    _chooseImage.contentMode = UIViewContentModeScaleAspectFit;
}

-(void)setEmbroidCellWithModel:(CustomSecondModel*)model
{
    CGFloat jianGeW = 15;
    CGFloat jianGeH = 5;
    [_valueLabel removeFromSuperview];
    _headImage.hidden = NO;
    _valueLabel.hidden = NO;
    _chooseImage.hidden = YES;
    _headImage.frame = CGRectMake(jianGeW, jianGeH, _item_Width-jianGeW*2, _item_Height-jianGeH*2);
    
//    [_headImage sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:model.imageUrl]];
    _headImage.userInteractionEnabled = NO;
    
    _nameLabel.text = model.name;
    _nameLabel.textColor = QianZi_color;
    _nameLabel.font = More_Little_Font;
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.numberOfLines = 3;
    [_nameLabel setBianKuangAndColor:[UIColor clearColor]];
    _nameLabel.frame = CGRectMake(jianGeW,5, _item_Width-jianGeW*2, 45);
    if (model.isChoose) {
        _headImage.image = [UIImage imageNamed:@"cixiu_xuan"];
    }else{
        _headImage.image = [UIImage imageNamed:@"cixiu_weixuan"];
    }
    _valueLabel = [[UILabel alloc]init];
    NSString * str = [NSString stringWithFormat:@"%@",model.valueWords];
    NSString * str2 = [NSString stringWithFormat:@"\n%@-%@",model.valueFont,model.valueColor];
    NSString * needStr = [NSString stringWithFormat:@"%@%@",str,str2];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:needStr];
    
    // 设置字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:11.0f]
                    range:NSMakeRange(0, needStr.length)];
    // 添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:QianZi_color
                    range:NSMakeRange(0, needStr.length)];
    // 添加文字颜色
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:ShenZi_Color
                    range:NSMakeRange(0, str.length)];
    // 设置字体和设置字体的范围
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:13.0f]
                    range:NSMakeRange(0, str.length)];
    if ([GlobalHandleValue(model.valueWords) isEqualToString:@""]|| GlobalHandleValue(model.valueWords) == nil) {
        attrStr = [[NSMutableAttributedString alloc] initWithString:@""];
    }
    _valueLabel.attributedText = attrStr;
    _valueLabel.numberOfLines = 2;
    _valueLabel.frame = CGRectMake(jianGeW+15, _nameLabel.frame.size.height+12,  _item_Width-jianGeW*2-10, 35);
  
    [self addSubview:_valueLabel];
}

-(void)setLabelCellWithModel:(CustomSecondModel*)model
{
    _nameLabel.frame = CGRectMake(0, (_item_Height-35)/2.0-10, _item_Width, 35);
    _nameLabel.text = model.name;
    _nameLabel.numberOfLines = 2;
    _nameLabel.font = More_Little_Font;
    _nameLabel.textColor = ShenZi_Color;
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    [_nameLabel setBianKuangAndColor:[UIColor clearColor]];
}
-(void)setNomalCellWithModel:(CustomSecondModel*)model
{
    CGFloat jianGe = 7;
    _headImage.hidden = NO;
    _headImage.frame = CGRectMake(jianGe, jianGe, _item_Width-jianGe*2, _item_Width-jianGe*2);
    [_headImage sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:model.imageUrl]];
    _nameLabel.text = model.name;
    _nameLabel.textColor = QianZi_color;
    _nameLabel.font = More_Little_Font;
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.numberOfLines = 2;
    [_nameLabel setBianKuangAndColor:[UIColor clearColor]];
    _nameLabel.frame = CGRectMake(0,_headImage.frame.origin.y+_headImage.frame.size.height+5, _item_Width, 35);
    if (model.isChoose) {
        _chooseImage.hidden = NO;
    }else{
        _chooseImage.hidden = YES;
    }
    _chooseImage.image = [UIImage imageNamed:@"colorXuan"];
    _chooseImage.frame = CGRectMake(jianGe*2+2, 0, _item_Width-jianGe*2-2, _item_Width-jianGe*2);
    _chooseImage.contentMode = UIViewContentModeScaleAspectFit;
}
-(void)setNameCellWithModel:(CustomSecondModel*)model
{
    _nameLabel.frame = CGRectMake(0, (_item_Height-35)/2.0, _item_Width, 35);
    _nameLabel.text = model.name;
    _nameLabel.numberOfLines = 2;
    _nameLabel.font = More_Little_Font;
    _nameLabel.textColor = ZhongZi_Color;
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    [_nameLabel setYuanJiaoForRadius:9.0 andMasks:NO];
    [_nameLabel setBianKuangAndColor:RGBColorAlpha(205, 205, 205, 1)];

    if (model.isChoose) {
        _chooseImage.hidden = NO;
        [_nameLabel setBianKuangAndColor:[UIColor clearColor]];
        
    }else{
        _chooseImage.hidden = YES;
    }
    _chooseImage.frame = _nameLabel.frame;
    CGFloat top = 10; // 顶端盖高度
    CGFloat bottom = 10 ; // 底端盖高度
    CGFloat left = 20; // 左端盖宽度
    CGFloat right = 30; // 右端盖宽度
    UIImage * image = [UIImage imageNamed:@"kuangxuanzhong"];
    // 指定为拉伸模式，伸缩后重新赋值
    image = [image stretchImageWithInsetsTop:top left:left bottom:bottom right:right];
    _chooseImage.image =image;
}

#pragma mark - textViewDelegate

-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    _model.value = textView.text;
    [textView resignFirstResponder];
    return YES;
}

@end
